package com.app.micaihu.view.user.userinfo.activity

import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Bitmap.CompressFormat
import android.graphics.RectF
import android.net.Uri
import android.os.Bundle
import android.view.MenuItem
import com.app.base.BaseActivity
import com.app.micaihu.R
import com.app.micaihu.configure.Config
import com.app.utils.util.file.AppFileUtils
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ImageUtils
import com.isseiaoki.simplecropview.callback.LoadCallback
import kotlinx.android.synthetic.main.user_header_corp_activity.*

/**
 * 头像裁剪
 */
class UserHeaderCorpActivity : BaseActivity() {
    override fun getLayoutId() = R.layout.user_header_corp_activity

    private val KEY_FRAME_RECT = "FrameRect"
    private val KEY_SOURCE_URI = "SourceUri"
    var mSourceUri: Uri? = null
    private val mCompressFormat = CompressFormat.JPEG
    private var mFrameRect: RectF? = null
    override fun initBar(initBar: Boolean) {
        super.initBar(false)
        BarUtils.setStatusBarColor(this, ColorUtils.getColor(R.color.color222))
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        if (savedInstanceState != null) {
            // restore data
            mFrameRect = savedInstanceState.getParcelable(KEY_FRAME_RECT)
            mSourceUri = savedInstanceState.getParcelable<Uri>(KEY_SOURCE_URI)
        }
        super.onCreate(savedInstanceState)

    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        // save data
        outState.putParcelable(KEY_FRAME_RECT, cropImageView.actualCropRect)
        outState.putParcelable(KEY_SOURCE_URI, cropImageView.sourceUri)
    }

    override fun setData() {

        intent.data?.let {
            // load image
            mSourceUri = it
            cropImageView.load(it)
                    .initialFrameScale(0.7f)
                    .initialFrameRect(mFrameRect)
                    .useThumbnail(true)
                    .execute(mLoadCallback)
        }
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true);//添加默认的返回图标
        supportActionBar?.setHomeButtonEnabled(true); //设置返回键可用

    }

    override fun addListeners() {
        tvOk.setOnClickListener {
            val bitmap = cropImageView.croppedBitmap
            if (bitmap != null) {
                val path = AppFileUtils.getImageFile(cacheDir.absolutePath)
                val save = ImageUtils.save(bitmap, path, Bitmap.CompressFormat.JPEG)
                if (save) {
                    val intent = Intent()
                    intent.putExtra(Config.Extra.path, path.absolutePath)
                    setResult(RESULT_OK, intent)
                }
                finish()
            }
        }
    }

    // Callbacks ///////////////////////////////////////////////////////////////////////////////////
    private val mLoadCallback: LoadCallback = object : LoadCallback {
        override fun onSuccess() {}
        override fun onError(e: Throwable) {}
    }

    //Toolbar的事件---返回
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            finish()
        }
        return super.onOptionsItemSelected(item)
    }
}